var api_url = $("#api_url").val();
var mh5Url = api_url+'';

var res = {}
    //模拟alert弹框
function alertnew(txt) {
    $('#monialert').remove();
    var _cont =
        ' <div id="monialert" style="background:#000; padding:10px 0;border-radius:20px;text-align:center;font-size:13px; color:#fff;z-index:999;opacity: 0.7;width:80%;position:fixed;top:100px;left:10%;">' +
        txt + '</div>';
    $("body").append(_cont);
    setTimeout(function() {
        $('#monialert').remove();
    }, 2000)
};
//借款金额失去焦点调用
$("#loan_money").bind('blur', function() {
    _check_money('loan_money', '借款金额');
});
//对私流水
$("#privateFlower").bind('blur', function() {
    _check_money('privateFlower', '每月对私流水');
});
//对公流水
$("#publicFlower").bind('blur', function() {
    _check_money('publicFlower', '每月对公流水');
});
//工资月收入inCome
$("#inCome").bind('blur', function() {
    _check_money('inCome', '工资月收入');
});
//检验金额
window._check_money = _check_money;

function _check_money(_money, _name) {
    var money = $('#' + _money);
    var money_val = money.val();
    var money_reg = /^\d{1,}$/;
    if (money_val == "") {
        alertnew(_name + "不能为空");
        return false;
    } else if (!money_reg.test(money_val)) {
        alertnew("请输入整数");
        return false;
    } else if (parseInt(money_val) < 1000) {
        alertnew(_name + "不得少于1000元");
        return false;
    } else if (parseInt(money_val) > 5000000) {
        alertnew(_name + "上限500W,您的月收入已超过500W");
        return false;
    } else if(parseInt(money_val)%1000!=0){
        alertnew(_name + "应该是1000的倍数!");
        return false;
    }
    return true;
}
/*验证姓名*/
$('#username').bind('blur', function() {
    _check_name('username');
});
window._check_name = _check_name;

function _check_name(_name) {
    var name = $('#' + _name);
    var name_val = name.val();
    var name_reg1 = /(^[\u4e00-\u9fa5]{2,8}$)/;
    var name_reg2 = /(^[a-zA-Z]+([ ][a-zA-Z]+){0,5}$)/;
    if (name_val == "") {
        alertnew("姓名不能为空");
        return false;
    }
    if (name_reg1.test(name_val) && name_val.length <= 8 && name_val.length >= 2) {
        return true;
    } else if (name_reg2.test(name_val) && name_val.length <= 16 && name_val.length >= 4) {
        return true;
    } else {
        alertnew("您的姓名无效");
        return false;
    }
    return true;
}

/*验证手机号*/
$('#tel').bind('blur', function() {
    _check_phone('tel');
});
window._check_phone = _check_phone;

function _check_phone(_tel) {
    var phone = $('#' + _tel);
    var phone_val = phone.val();
    var phone_reg = /^1[3|4|5|8|7][0-9]\d{8,8}$/;
    if (phone_val == '') {
        alertnew('手机号码不能为空');
        return false;
    } else if (!phone_reg.test(phone_val)) {
        alertnew('手机号码格式不正确');
        return false;
    }
    return true;
}

//验证码是否是数字以及长度四位的校验
$('#yzm').bind('blur', function() {
    _check_code('yzm');
})
window._check_code = _check_code;

function _check_code(_code) {
    var code = $('#' + _code);
    var code_val = code.val();
    var code_reg = /^\d{4}$/;
    if (code_val == '') {
        alertnew('验证码不能为空');
        return false;
    } else if (!code_reg.test(code_val)) {
        alertnew('验证码格式不正确');
        return false;
    }
    return true;
}
/*验证身份证号*/
$('#idCard1').bind('blur', function() {
    IdentityCodeValid($('#idCard1').val());
});
$('#idCard2').bind('blur', function() {
    IdentityCodeValid($('#idCard2').val());
});

window.IdentityCodeValid = IdentityCodeValid;

function IdentityCodeValid(code) {
    var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
    var tip = "";
    var pass = true;
    if (code == "") {
        alertnew("身份证号不能为空");
        return false;
    } else if (!code || !/^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/i.test(code)) {
        //tip = "身份证号格式错误";
        alertnew("身份证号格式错误");
        return false;
    } else if (!city[code.substr(0, 2)]) {
        //tip = "地址编码错误";
        alertnew("地址编码错误");
        return false;
    } else {
        //18位身份证需要验证最后一位校验位
        if (code.length == 18) {
            code = code.split('');
            //∑(ai×Wi)(mod 11)
            //加权因子
            var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
            //校验位
            var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
            var sum = 0;
            var ai = 0;
            var wi = 0;
            for (var i = 0; i < 17; i++) {
                ai = code[i];
                wi = factor[i];
                sum += ai * wi;
            }
            var last = parity[sum % 11];
            if (last != code[17] && code[17] != "x") {
                alertnew("身份证校验错误");
                return false;
            }
        }
    }
    return true;
}
//检查城市
window._checkCity=_checkCity;
function _checkCity(cityName,name){
    var city=parseInt($(cityName).attr('data-id'))
    if(city==0) {
        alertnew('请选择'+name+'城市');
        return false;
    }else{
        return true;
    }
}


//贷款期限
window.sel = sel;

function sel(nameClass, name) {
    var loan = parseInt($(nameClass).attr('data-val'));
    if (loan == -1) {
        alertnew('请选择' + name);
        return false;
    } else {
        return true;
    }
}

$("#loan_money").keyup(function () {
    if($("#loan_money").val() >= 10000){
        $("#jiekuan_div").show();
        $("#leixing_div").show();
    }else{
        $("#jiekuan_div").hide();
        $("#leixing_div").hide();
    }
})
//协议
$(".xy em").bind('click',function() {
    $(this).toggleClass("selOn");
});

//协议
$(".xy1 em").bind('click',function() {
    $(this).toggleClass("selOn");
});

//协议
$(".xy2 em").bind('click',function() {
    $(this).toggleClass("selOn");
});

$('#btn1').bind('click', function() {

    if($(".xy em").prop("className") == "sel1 selOn")
    {
        alertnew('请勾选协议!');
        return false;
    }

    if (_check_money('loan_money', '借款金额') && sel('.msg.dkqx', '借款期限年')&& sel('.msg.dkqx2', '借款期限月')&& _check_name('username') && _check_phone('tel') && _check_code('yzm') && sel('.msg.dklx', '贷款类型')) {
        if(parseInt($('.msg.dkqx').attr('data-val')) == 0 && parseInt($('.msg.dkqx2').attr('data-val')) == 0)
        {
            alertnew('贷款期限选择错误!');
            return false;
        }

        $(".loading_cover").show();
        res['step'] = $("#step").val();
        res['money'] = $('#loan_money').val();
        res['month'] = parseInt($('.msg.dkqx').attr('data-val'))*12+parseInt($('.msg.dkqx2').attr('data-val'));
        res['username'] = $('#username').val();
        res['mobile'] = $('#tel').val();
        res['verify_code'] = $("#yzm").val();
        res['type'] = parseInt($('.msg.dklx').attr('data-val'));
        res['loan_name'] = 'aff';
        res['channel'] = $("#channel").val();
        if($(".xy1 em").prop("className") == "sel1 selOn" || $(".xy2 em").prop("className") == "sel1 selOn")
        {
            res['insurance'] = 0;
        }else{
            res['insurance'] = 1;
        }
        $.post(mh5Url, res, function(data) {
            if (data.rs_code == 1000) {
                $("#step").val(data.details.step);
                var stepObj = "step_"+data.details.step;
                $("#"+stepObj).attr('data-id',data.details.iu_id);
                $('.contain1').hide();
                $('.contain2').show();
                $(".loading_cover").hide();

                if(data.details.url==''){
                    var loan = parseInt($('.msg.dklx').attr('data-val'));
                    if (loan == 2) {
                        $('.enterprise1').show();
                        $('.personal1').hide();
                    } else if (loan == 1) {
                        $('.enterprise1').hide();
                        $('.personal1').show();
                    }
                }else{
                    $('.main').hide();
                    if(data.details.sdwap != 1){
                        $("#gerenppsuccess").show();
                        $("#ppurl").attr('href',data.details.url,'_blank');
                    }else{
                        $("#sdgerenppsuccess").show();
                        $("#sdurl").attr('href',data.details.url,'_blank');
                    }
                    setTimeout(function () {
                        window.location.href=data.details.url;
                    }, 2000);
                    return false;
                }

            }else{
                $(".loading_cover").hide();
                alertnew(data.rs_msg);
            }
        },'json')
    } else {
        return false;
    }
})

$('#btn2').bind('click', function() {
    $(".loading_cover").hide();
    var loan = parseInt($('.msg.dklx').attr('data-val'));
    var res = {};
    res['step'] = $("#step").val();
    res['iu_id'] = $("#step_"+$("#step").val()).attr('data-id');
    res['loan_name'] = 'aff';
    res['channel'] = $("#channel").val();
    res['type'] = parseInt($('.msg.dklx').attr('data-val'));

    if($(".xy1 em").prop("className") == "sel1 selOn" || $(".xy2 em").prop("className") == "sel1 selOn")
    {
        res['insurance'] = 0;
    }else{
        res['insurance'] = 1;
    }
    if (loan == 2) {
        if (sel('.msg.qyfc', '名下房产情况') && sel('.msg.cc', '名下车产情况') && _check_money('privateFlower', '每月对私流水') && _check_money('publicFlower', '每月对公流水') && sel('.msg.yyzz', '营业执照年')&& sel('.msg.yyzz', '营业执照月') &&_checkCity('#cityone','企业所在地') && sel('.msg.qylx', '企业类型')) {

            if(parseInt($('.msg.yyzz').attr('data-val')) == 0 && parseInt($('.msg.yyzz1').attr('data-val')) == 0)
            {
                alertnew('营业执照选择错误!');
                return false;
            }

            $(".loading_cover").show();
            res['house_type'] = parseInt($('.msg.qyfc').attr('data-val'));
            res['car_type'] = parseInt($('.msg.cc').attr('data-val'));
            res['salary_bank_private'] = $('#privateFlower').val();
            res['salary_bank_public'] = $('#publicFlower').val();
            res['work_license'] = parseInt($('.msg.yyzz').attr('data-val'))*12+parseInt($('.msg.yyzz1').attr('data-val'));
            res['zone_id'] = $('#cityone').attr('data-id');
            res['use_compnay'] = parseInt($('.msg.qylx').attr('data-val'));
            $.post(mh5Url, res, function(data) {
                if (data.rs_code == 1000) {
                    $("#step").val(data.details.step);
                    var stepObj = "step_"+data.details.step;
                    $("#"+stepObj).attr('data-id',data.details.iu_id);

                    $('.contain1').hide();
                    $('.contain2').hide();
                    $('.contain3').show();
                    $('.enterprise2').show();
                    $(".loading_cover").hide();
                } else {
                    $(".loading_cover").hide();
                    alertnew(data.rs_msg);
                }
            })

        } else {
            return false;
        }
    } else if (loan == 1) {
        if (sel('.msg.dkyt', '贷款用途') && _checkCity('#citytwo','企业所在地') && _check_money('inCome', '工资月收入') && sel('.msg.gzffyx', '工资发放形式') && sel('.msg.gzsj', '当前公司工作年')&& sel('.msg.gzsj1', '当前公司工作月') && sel('.msg.sfyxyk', '是否有信用卡')) {

            if(parseInt($('.msg.gzsj').attr('data-val')) == 0 && parseInt($('.msg.gzsj1').attr('data-val')) == 0)
            {
                alertnew('当前工作时间选择错误!');
                return false;
            }

            $(".loading_cover").show();
            res['use_compnay'] = parseInt($('.msg.dkyt').attr('data-val'));
            res['salary_bank_public'] = $('#inCome').val();
            res['salary_bank_private'] = parseInt($('.msg.gzffyx').attr('data-val'));
            res['work_license'] = parseInt($('.msg.gzsj').attr('data-val'))*12+parseInt($('.msg.gzsj1').attr('data-val'));
            res['credit_card'] = parseInt($('.msg.sfyxyk').attr('data-val'));
            res['zone_id']=$('#citytwo').attr('data-id');
            $.post(mh5Url, res, function(data) {
                if (data.rs_code == 1000) {
                    $("#step").val(data.details.step);
                    var stepObj = "step_"+data.details.step;
                    $("#"+stepObj).attr('data-id',data.details.iu_id);

                    $('.contain1').hide();
                    $('.contain2').hide();
                    $('.contain3').show();
                    $('.personal2').show();
                    $(".loading_cover").hide();
                } else {
                    $(".loading_cover").hide();
                    alertnew(data.rs_msg);
                }
            })
        } else {
            return false;
        }
    }
})

$('#btn3').bind('click', function() {
    var loan = parseInt($('.msg.dklx').attr('data-val'));
    var res = {};
    res['step'] = $("#step").val();
    res['iu_id'] = $("#step_"+$("#step").val()).attr('data-id');
    res['loan_name'] = 'aff';
    res['channel'] = $("#channel").val();
    res['type'] = parseInt($('.msg.dklx').attr('data-val'));

    if($(".xy1 em").prop("className") == "sel1 selOn" || $(".xy2 em").prop("className") == "sel1 selOn")
    {
        res['insurance'] = 0;
    }else{
        res['insurance'] = 1;
    }
    if (loan == 2) {
        if (sel('.msg.fzqk', '负债情况') && sel('.msg.gshy', '公司行业') && IdentityCodeValid($('#idCard1').val()) && sel('.msg.hyzk', '婚姻状况') && sel('.msg.dkjl', '有无贷款成功记录')) {
            $(".loading_cover").show();
            res['has_debt'] = parseInt($('.msg.fzqk').attr('data-val'));
            res['industry'] = parseInt($('.msg.gshy').attr('data-val'));
            res['iden_card'] = $('#idCard1').val();
            res['marriage'] = parseInt($('.msg.hyzk').attr('data-val'));
            res['has_loan'] = parseInt($('.msg.dkjl').attr('data-val'));
            $.post(mh5Url, res, function(data) {
                if (data.rs_code == 1000) {
                    $("#step").val(data.details.step);
                    var stepObj = "step_"+data.details.step;
                    $("#"+stepObj).attr('data-id',data.details.iu_id);

                    $('.contain1').hide();
                    $('.contain2').hide();
                    $('.contain3').hide();
                    $('.contain4').show();
                    $(".loading_cover").hide();
                    $("#done_money").text($('#loan_money').val());
                    $("#done_month").text(parseInt($('.msg.dkqx').attr('data-val'))*12+parseInt($('.msg.dkqx2').attr('data-val')));
                    /*回跳机构*/
                    if($("#ref_flag").val() == 1)
                    {
                        write = $("#reffer_waite").val();
                        alertnew('等待'+write/1000+'秒自动跳转回原网址');
                        setTimeout(function () {
                            window.location.href=$("#http_referer").val();
                        },write)
                    }
                } else {
                    $(".loading_cover").hide();
                    alertnew(data.rs_msg);
                }
            })
        } else {
            return false;
        }
    } else if (loan == 1) {
        if (sel('.msg.grfc', '名下房产情况') && sel('.msg.cc', '名下车产情况') && sel('.msg.gjj', '连续半年缴纳公积金情况') && sel('.msg.sb', '连续半年缴纳社保情况') && sel('.msg.hyzk', '婚姻状况') && IdentityCodeValid($('#idCard2').val()) && sel('.msg.zysf', '职业身份')) {
            $(".loading_cover").hide();
            res['house_type'] = parseInt($('.msg.grfc').attr('data-val'));
            res['car_type'] = parseInt($('.msg.cc').attr('data-val'));
            res['is_fund'] = parseInt($('.msg.gjj').attr('data-val'));
            res['is_security'] = parseInt($('.msg.sb').attr('data-val'));
            res['marriage'] = parseInt($('.msg.hyzk').attr('data-val'));
            res['iden_card'] = $('#idCard2').val();
            res['profession'] = parseInt($('.msg.zysf').attr('data-val'));
            $.post(mh5Url, res, function(data) {
                if (data.rs_code == 1000) {
                    $("#step").val(data.details.step);
                    var stepObj = "step_"+data.details.step;
                    $("#"+stepObj).attr('data-id',data.details.iu_id);

                    $('.contain1').hide();
                    $('.contain2').hide();
                    $('.contain3').hide();
                    $('.contain4').show();
                    $(".loading_cover").hide();
                    $("#done_money").text($('#loan_money').val());
                    $("#done_month").text(parseInt($('.msg.dkqx').attr('data-val'))*12+parseInt($('.msg.dkqx2').attr('data-val')));
                    /*回跳机构*/
                    if($("#ref_flag").val() == 1)
                    {
                        write = $("#reffer_waite").val();
                        alertnew('等待'+write/1000+'秒自动跳转回原网址');
                        setTimeout(function () {
                            window.location.href=$("#http_referer").val();
                        },write)
                    }
                } else {
                    $(".loading_cover").hide();
                    alertnew(data.rs_msg);
                }
            })
        } else {
            return false;
        }
    }
})